package com.bootdo.mall.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.mall.domain.MallCateGory;
import com.bootdo.mall.domain.MallGood;
import com.bootdo.mall.service.MallCateGoryService;
import com.bootdo.mall.service.MallGoodService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.mall.domain.MallIndexCategoryJoin;
import com.bootdo.mall.service.MallIndexCategoryJoinService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author lujingqian
 * @email 2100272728@qq.com
 * @date 2019-11-12 14:57:08
 */
 
@Controller
@RequestMapping("/mall/mallIndexCategoryJoin")
public class MallIndexCategoryJoinController extends BaseController{


	@Autowired
	private MallIndexCategoryJoinService mallIndexCategoryJoinService;

	@Autowired
	private MallGoodService mallGoodService;

	@Autowired
	private MallCateGoryService mallCateGoryService;

	@ModelAttribute
	public MallIndexCategoryJoin get(@RequestParam(required=false) Long id) {
		if (id != null){
			return mallIndexCategoryJoinService.get(id);
		}else{
			return new MallIndexCategoryJoin();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/mall/mallIndexCategoryJoin','mall:mallIndexCategoryJoin:mallIndexCategoryJoin',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','mall:mallIndexCategoryJoin:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("mall:mallIndexCategory:edit")
	String MallIndexCategoryJoin(MallIndexCategoryJoin mallIndexCategoryJoin,HttpServletRequest request,HttpServletResponse response){
	    return "mall/mallIndexCategoryJoin/mallIndexCategoryJoin";
	}
	
	/*@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("mall:mallIndexCategory:edit")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MallIndexCategoryJoin> mallIndexCategoryJoinList = mallIndexCategoryJoinService.list(query);
		int total = mallIndexCategoryJoinService.count(query);
		mallIndexCategoryJoinList.stream().forEach(item ->{
			if(item.getType().intValue() == 1){
				MallCateGory mallCateGory =  mallCateGoryService.get(item.getRefId().intValue());
				if(null != mallCateGory){
					item.setName(mallCateGory.getName());
					item.setPic(mallCateGory.getPic());
				}
			}else{
				MallGood mallGood  = mallGoodService.getMin(item.getRefId());
				if(null != mallGood){
					item.setName(mallGood.getName());
					item.setPic(mallGood.getPic());
				}
			}
		});
		PageUtils pageUtils = new PageUtils(mallIndexCategoryJoinList, total);
		return pageUtils;
	}*/

	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("mall:mallIndexCategory:edit")
	public R list(@RequestParam Map<String, Object> params){
		List<MallIndexCategoryJoin> mallIndexCategoryJoinList = mallIndexCategoryJoinService.list(params);
		mallIndexCategoryJoinList.stream().forEach(item ->{
			if(item.getType().intValue() == 1){
				MallCateGory mallCateGory =  mallCateGoryService.get(item.getRefId().intValue());
				if(null != mallCateGory){
					item.setName(mallCateGory.getName());
					item.setPic(mallCateGory.getPic());
				}
			}else{
				MallGood mallGood  = mallGoodService.getMin(item.getRefId());
				if(null != mallGood){
					item.setName(mallGood.getName());
					item.setPic(mallGood.getPic());
				}
			}
		});

		return R.ok().put("l", mallIndexCategoryJoinList);
	}
	@GetMapping("/add")
	@RequiresPermissions("mall:mallIndexCategory:edit")
	String add(MallIndexCategoryJoin mallIndexCategoryJoin,HttpServletRequest request,HttpServletResponse response){
	    return "mall/mallIndexCategoryJoin/mallIndexCategoryJoinedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(MallIndexCategoryJoin mallIndexCategoryJoin) {
		return R.ok().put("i",mallIndexCategoryJoin);
	}




	@PostMapping("/updatePx")
	@ResponseBody
	R updatePx(@RequestParam("id")Long id,@RequestParam("px")Integer px) {
		MallIndexCategoryJoin mallIndexCategoryJoin = mallIndexCategoryJoinService.get(id);
		mallIndexCategoryJoin.setPx(px);
		mallIndexCategoryJoinService.save(mallIndexCategoryJoin);
		return R.ok();
	}




	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallIndexCategory:edit")
	public R save( MallIndexCategoryJoin mallIndexCategoryJoin){
		//类型为2时 为商品 1是分类
		if(mallIndexCategoryJoin.getType().intValue() == 2){
			MallIndexCategoryJoin temp = 	mallIndexCategoryJoinService.getIndexCategoryIdAndTypeAndRefId(mallIndexCategoryJoin.getType(),mallIndexCategoryJoin.getIndexCategoryId(),mallIndexCategoryJoin.getRefId());
			if(null != temp){
				temp.setPx(mallIndexCategoryJoin.getPx());
				mallIndexCategoryJoin = temp;
			}
			mallIndexCategoryJoinService.removeByTypeIdAndIndexCategoryId(1, mallIndexCategoryJoin.getIndexCategoryId());
		}else{
			List<MallIndexCategoryJoin>  list =  mallIndexCategoryJoinService.
					list(MapBuild.getInstand().setKV("type", 1).
							setKV("indexCategoryId",mallIndexCategoryJoin.getIndexCategoryId()));
			if(list.size() > 0){
				if(list.get(0).getRefId().longValue() != mallIndexCategoryJoin.getRefId()){
					return  R.error("只能关联一个分类,请先删除已关联的分类");
				}else {
					list.get(0).setPx(mallIndexCategoryJoin.getPx());
					mallIndexCategoryJoin = list.get(0);
				}
			}
			mallIndexCategoryJoinService.removeByTypeIdAndIndexCategoryId(2, mallIndexCategoryJoin.getIndexCategoryId());
		}
		if(mallIndexCategoryJoinService.save(mallIndexCategoryJoin)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:mallIndexCategory:edit")
	public R remove( Long id){
		if(mallIndexCategoryJoinService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:mallIndexCategory:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		mallIndexCategoryJoinService.batchRemove(ids);
		return R.ok();
	}
	
}